package com.tech.service;

import cn.hutool.core.io.FileUtil;
import com.tech.model.GlobalConfigEntity;
import com.tech.model.TemplateEntity;
import com.tech.repo.GlobalConfigEntityRepository;
import com.tech.repo.TemplateEntityRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Created by zouyao on 2023/06/06
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class InitializeService {

    private final     TemplateEntityRepository     templateRepo;
    private final     GlobalConfigEntityRepository globalConfigRepo;

    public void init(){
        long globalConfigCount = globalConfigRepo.count();
        if (globalConfigCount == 0){
            log.info("start init globalConfig");
            globalConfigRepo.saveAndFlush(new GlobalConfigEntity("protocol","json","json,xml"));
            globalConfigRepo.saveAndFlush(new GlobalConfigEntity("log","true","保存生成记录"));
            globalConfigRepo.saveAndFlush(new GlobalConfigEntity("package","com.demo","包名"));
            globalConfigRepo.saveAndFlush(new GlobalConfigEntity("author","bridge-framework","作者"));
            log.info("init globalConfig success!!!");
        }

        long templateCount = templateRepo.count();
        if (templateCount == 0){
            log.info("start init template");
            templateRepo.saveAndFlush(new TemplateEntity("dto-json","","dto,json", FileUtil.readUtf8String("dto-json.ftl")));
            templateRepo.saveAndFlush(new TemplateEntity("dto-xml","","dto,xml", FileUtil.readUtf8String("dto-xml.ftl")));
            templateRepo.saveAndFlush(new TemplateEntity("stub","","stub",FileUtil.readUtf8String("stub.ftl")));
            log.info("init template success!!!");
        }
    }
}
